<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import { tabsList } from "./helps";
import { getDepartments } from "./servers";

import CloudManager from "./components/CloudManager/index.vue";
import CloudReport from "./components/CloudReport/index.vue";
import CloudQuickSearch from "./components/CloudQuickSearch/index.vue";
import CloudAssets from "./components/CloudAssets/index.vue";
import CloudTasks from "./components/CloudTasks/index.vue";
import CloudScene from "./components/CloudScene/index.vue";
import CloudCache from "./components/CloudCache/index.vue";
import CloudDepartment from "./components/CloudDepartment/index.vue";

const route = useRoute();
const activeTab = ref("0");

// 获取URL参数中的task
const taskParam = ref((route.query.task as string) || "");

// 部门列表数据
const departmentOptions = ref<Array<{ label: string; value: string }>>([]);

// 获取部门列表
const loadDepartments = async () => {
  try {
    const result: any = await getDepartments();
    if (result && result.departments && Array.isArray(result.departments)) {
      departmentOptions.value = result.departments;
    }
  } catch (error: any) {
    console.error("获取部门列表失败:", error);
    ElMessage.error("获取部门列表失败");
  }
};

// 页面加载时获取部门列表
onMounted(() => {
  loadDepartments();
});
</script>

<template>
  <div>
    <el-tabs type="border-card" v-model="activeTab">
      <el-tab-pane
        v-for="item in tabsList"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
        <div
          v-if="taskParam"
          style="display: flex; justify-content: space-between"
        >
          <div class="task-name">任务名称：{{ taskParam }}</div>
        </div>

        <!-- 网盘统计报表 -->
        <CloudReport
          :task="taskParam"
          :department-options="departmentOptions"
          v-if="item.key === 'cloudReport'"
        />
        <!-- 网盘管理 -->
        <CloudManager
          :task="taskParam"
          :department-options="departmentOptions"
          v-if="item.key === 'cloud'"
        />
        <!-- 网盘快速搜索 -->
        <CloudQuickSearch
          :task="taskParam"
          :department-options="departmentOptions"
          v-if="item.key === 'cloudQuickSearch'"
        />
        <!-- 网盘资产 -->
        <CloudAssets
          :task="taskParam"
          :department-options="departmentOptions"
          v-if="item.key === 'cloudAssets'"
        />
        <!-- 网盘任务统计 -->
        <CloudTasks
          :task="taskParam"
          :department-options="departmentOptions"
          v-if="item.key === 'cloudTask'"
        />
        <!-- 网盘场景配置 -->
        <CloudScene
          :task="taskParam"
          :department-options="departmentOptions"
          v-if="item.key === 'CloudScene'"
        />
        <!-- 网盘缓存管理 -->
        <CloudCache
          :task="taskParam"
          :department-options="departmentOptions"
          v-if="item.key === 'CloudCache'"
        />
        <!-- 网盘部门管理 -->
        <CloudDepartment
          :task="taskParam"
          v-if="item.key === 'CloudDepartment'"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<style lang="scss" scoped>
.search-form-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.search-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

.el-btn-zese {
  background: #722ed1;
  color: white;
  border-color: #722ed1;
}

.el-btn-zese.is-disabled,
.el-btn-zese.is-disabled:focus,
.el-btn-zese.is-disabled:hover {
  background: #722ed1;
  color: white;
  border-color: #722ed1;
}
.task-name {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 10px;
  border: 1px solid #e0e0e0;
  padding: 10px;
  border-radius: 5px;
}
</style>
